﻿/*

Copyright 2009 Guillaume Amringer

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

package {
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import flash.display.MovieClip;
	import events.InterfaceEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.AntiAliasType;
	import utils.SexUtils;

	public class LoadingInterface extends Interface {
		
		private var _assetsLoader:BulkLoader = new BulkLoader('loadingInterfaceLoader');
		
		private var _tl:MovieClip = new MovieClip();
		private var _tr:MovieClip = new MovieClip();
		private var _bl:MovieClip = new MovieClip();
		private var _br:MovieClip = new MovieClip();
		private var _top:MovieClip = new MovieClip();
		private var _bottom:MovieClip = new MovieClip();
		private var _left:MovieClip = new MovieClip();
		private var _right:MovieClip = new MovieClip();
		
		private var _bytesTotalTF:TextField = new TextField();
		private var _bytesLoadedTF:TextField = new TextField();
		private var _itemsTotalTF:TextField = new TextField();
		private var _itemsLoadedTF:TextField = new TextField();
		
		private var _loadingBar:LoadingBar = new LoadingBar(10, 240);
		
		public function LoadingInterface():void {
			_assetsLoader.add('interface/loading/tl.png',{id:'tl'});
			_assetsLoader.add('interface/loading/tr.png',{id:'tr'});
			_assetsLoader.add('interface/loading/bl.png',{id:'bl'});
			_assetsLoader.add('interface/loading/br.png',{id:'br'});
			_assetsLoader.add('interface/loading/top.png',{id:'top'});
			_assetsLoader.add('interface/loading/bottom.png',{id:'bottom'});
			_assetsLoader.add('interface/loading/left.png',{id:'left'});
			_assetsLoader.add('interface/loading/right.png',{id:'right'});
			
			_assetsLoader.addEventListener(BulkProgressEvent.COMPLETE, _assetsLoadingCompleteHandler);
		}
		
		public override function build():void {
			
			_bytesTotalTF.defaultTextFormat = globals.smallTFormat;
			_bytesTotalTF.autoSize = TextFieldAutoSize.RIGHT
			_bytesTotalTF.antiAliasType = AntiAliasType.ADVANCED;
			
			_bytesLoadedTF.defaultTextFormat = globals.smallTFormat;
			_bytesLoadedTF.autoSize = TextFieldAutoSize.RIGHT
			_bytesLoadedTF.antiAliasType = AntiAliasType.ADVANCED;
			
			_itemsTotalTF.defaultTextFormat = globals.smallTFormat;
			_itemsTotalTF.autoSize = TextFieldAutoSize.RIGHT
			_itemsTotalTF.antiAliasType = AntiAliasType.ADVANCED;
			
			_itemsLoadedTF.defaultTextFormat = globals.smallTFormat;
			_itemsLoadedTF.autoSize = TextFieldAutoSize.RIGHT
			_itemsLoadedTF.antiAliasType = AntiAliasType.ADVANCED;
			
			_assetsLoader.start();
		}
		
		public override function resize():void {
			
			//	Top bar
			_top.x = _tl.width;
			_top.y = 0;
			_top.width = globals.width - _tl.width - _tr.width;
			
			//	Bottom bar
			_bottom.x = _bl.width;
			_bottom.y = globals.height - _bottom.height;
			_bottom.width = globals.width - _bl.width - _br.width;
			
			//	Left bar
			_left.x = 0;
			_left.y = _tl.height;
			_left.height = globals.height - _tl.height - _bl.height;
			
			//	Right bar
			_right.x = globals.width - _right.width;
			_right.y = _tr.height;
			_right.height = globals.height - _tr.height - _br.height;
			
			//	Top left corner
			_tl.x = 0;
			_tl.y = 0;
			
			//	Top right corner
			_tr.x = globals.width - _tr.width;
			_tr.y = 0;
			
			//	Bottom left corner
			_bl.x = 0;
			_bl.y = globals.height - _bl.height;
			
			//	Bottom right corner
			_br.x = globals.width - _br.width;
			_br.y = globals.height - _br.height;
			
			//	Info TextFields
			_itemsLoadedTF.x = 150;
			_itemsLoadedTF.y = globals.height - 100;
			_itemsTotalTF.x = 150;
			_itemsTotalTF.y = globals.height - 80;
			_bytesLoadedTF.x = 150;
			_bytesLoadedTF.y = globals.height - 60;
			_bytesTotalTF.x = 150;
			_bytesTotalTF.y = globals.height - 40;
			
			//	Loading bar
			_loadingBar.x = globals.width - 10;
			_loadingBar.y = globals.height - 50;
			_loadingBar.rotation = 180;
		}
		
		public function updatePercentage(pBytesTotal:int, pBytesLoaded:int, pItemsTotal:int, pItemsLoaded:int, pSpeed:Number, pPercentage:Number):void {
			_bytesTotalTF.text = SexUtils.units(pBytesTotal,'B');
			_bytesLoadedTF.text = SexUtils.units(pBytesLoaded,'B');
			_itemsTotalTF.text = pItemsTotal.toString() + ' Items';
			_itemsLoadedTF.text = pItemsTotal.toString() + ' Items';
			
			_loadingBar.updatePercentage(pPercentage);
		}
		
		private function _assetsLoadingCompleteHandler(e:BulkProgressEvent):void {
			_tl.addChild(_assetsLoader.getBitmap('tl'));
			_tr.addChild(_assetsLoader.getBitmap('tr'));
			_bl.addChild(_assetsLoader.getBitmap('bl'));
			_br.addChild(_assetsLoader.getBitmap('br'));
			_top.addChild(_assetsLoader.getBitmap('top'));
			_bottom.addChild(_assetsLoader.getBitmap('bottom'));
			_left.addChild(_assetsLoader.getBitmap('left'));
			_right.addChild(_assetsLoader.getBitmap('right'));
			
			this._constructInterface();
		}
		
		private function _constructInterface():void {
			
			this.resize();
			this.addChild(_top);
			this.addChild(_bottom);
			this.addChild(_left);
			this.addChild(_right);
			this.addChild(_tl);
			this.addChild(_tr);
			this.addChild(_bl);
			this.addChild(_br);
			
			this.addChild(_itemsLoadedTF);
			this.addChild(_itemsTotalTF);
			this.addChild(_bytesLoadedTF);
			this.addChild(_bytesTotalTF);
			
			this.addChild(_loadingBar);
			
			this.dispatchEvent(new InterfaceEvent(InterfaceEvent.READY));
		}
	}
}